package com.emep.changzhi.analyse.service.basics;

import com.emep.changzhi.analyse.entity.basics.District;
import com.emep.changzhi.analyse.repository.basics.DistrictRepository;
import com.emep.changzhi.analyse.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: LFY
 * @version:V1.0
 * @Description:
 * @Date: 2018/9/5 17:30
 */
@Service
public class DistrictService {
    @Autowired
    DistrictRepository districtRepository;

    public Page<District> find(int pageIndex, int pageSize, Map<String, Object> params) {

        Page<District> departmentPage = districtRepository.findAll(new DistrictService.DistrictSpec(params),
                PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "name"));
        return departmentPage;
    }

    public boolean add(District district) {
        List<District> infos =districtRepository.findAllByNameAndIsDelete(district.getName(), Constant.RESULT_DELETECODE_NO);
        if(infos.isEmpty()){
            districtRepository.save(district);
            return true;
        }
        return false;

    }

    public District getDistrict(long districtId){
        return districtRepository.findByDistrictId(districtId); }

    public boolean deleteDistrict(District district){
        District department = districtRepository.save(district);
        if(department != null){
            return true;
        }else {
            return false;
        }
    }

    public boolean updateDepartment(District district){
        List<District> infos =districtRepository.findAllByNameAndDistrictIdNotAndIsDelete(district.getName(),district.getDistrictId(),Constant.RESULT_DELETECODE_NO);
        if(infos.isEmpty()){
            districtRepository.save(district);
            return true;
        }
        return false;
    }

    private class DistrictSpec implements Specification<District> {
        private Map<String, Object> params = new HashMap<String, Object>();

        public DistrictSpec(Map<String, Object> params) {
            this.params = params;
        }

        @Override
        public Predicate toPredicate(Root<District> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            List<Predicate> lstPredicates = new ArrayList<Predicate>();

            if (params.containsKey("epaId")) {
                lstPredicates.add(cb.equal(root.get("epaId").as(String.class), params.get("epaId").toString()));
            }
            if (params.containsKey("leaderId")) {
                lstPredicates.add(cb.equal(root.get("leaderId").as(String.class), params.get("leaderId").toString()));
            }
            if (params.containsKey("name")) {
                lstPredicates.add(cb.like(root.get("name").as(String.class), params.get("name").toString()));
            }
            if (params.containsKey("isDelete")) {
                lstPredicates.add(cb.like(root.get("isDelete").as(String.class), params.get("isDelete").toString()));
            }

            Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
            return cb.and(lstPredicates.toArray(arrayPredicates));
        }
    }
}
